package com.wzapp.sitposture.authentication.interceptor;

import com.wzapp.sitposture.common.base.SpringContextHolder;
import com.wzapp.sitposture.config.H5Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 功能: 跨域处理拦截器. <br/>
 *
 * @author ql
 * @date 2018-01-20 11:59
 * @version
 * @since JDK 1.8
 */
public class CorsInterceptor implements HandlerInterceptor {

    private static final Logger log = LoggerFactory.getLogger(CorsInterceptor.class);

    H5Properties h5Properties = SpringContextHolder.getBean("h5Properties");

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // 处理跨域问题
        String h5Server = h5Properties.getH5domain();
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods","GET,POST,OPTIONS,PATCH,DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        // Authorization 就是允许这样的Header名的请求进行跨域
        response.setHeader("Access-Control-Allow-Headers", "Authorization, Origin, X-Requested-With, Content-Type, Accept, Connection, User-Agent, Cookie, channel, token, city, deviceInfo");

        return true;
    }


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
